package com.ruoyi.mobile.api;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageSerializable;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResults;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.qsng.domain.*;
import com.ruoyi.qsng.service.IKClearinghouseCommentReplyService;
import com.ruoyi.qsng.service.IKClearinghouseCommentService;
import com.ruoyi.qsng.service.IKClearinghouseLikeService;
import com.ruoyi.qsng.service.IKClearinghouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;

/**
 * 交流中心
 *
 * @author gk
 * @date 2023-06-14
 */
@Api(tags = "交流中心")
@RestController
@RequestMapping
public class ClearingHouseController extends BaseController {

    @Autowired
    private IKClearinghouseService kClearinghouseService;

    @Autowired
    private IKClearinghouseLikeService kClearinghouseLikeService;

    @Autowired
    private IKClearinghouseCommentService kClearinghouseCommentService;

    @Autowired
    private IKClearinghouseCommentReplyService kClearinghouseCommentReplyService;


    @ApiOperation(value = "交流中心查询列表")
    @GetMapping("/weixin/clearingHouse/list")
    public AjaxResults<List<KClearinghouse>> list(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,KClearinghouse kClearinghouse)
    {
        kClearinghouse.setState(String.valueOf(1));
        //查询本人点赞的交流
        KClearinghouseLike kClearinghouseLike =new KClearinghouseLike();
        Long id = SecurityUtils.getLoginUser().getUser().getUserId();
        Assert.notNull(id, "获取用户id失败！");
        kClearinghouseLike.setUserId(String.valueOf(id));
        kClearinghouseLike.setIsLike((long) 1);
        List<KClearinghouseLike> kClearinghouseLikes=kClearinghouseLikeService.selectKClearinghouseLikeList(kClearinghouseLike);
        HashSet<Object> set = new HashSet<>();
        for (KClearinghouseLike kClearinghouseLike1: kClearinghouseLikes) {
            set.add(kClearinghouseLike1.getClearinghouseId());
        }
        PageHelper.startPage(pageNum, pageSize);
        List<KClearinghouse> list =kClearinghouseService.selectKClearinghouseListForAPP(kClearinghouse);
        for (KClearinghouse kClearinghouse1: list) {
            kClearinghouse1.setIsLike(false);
            if(set.contains(kClearinghouse1.getId())){
                kClearinghouse1.setIsLike(true);
            }
        }
        return AjaxResults.success(new PageSerializable<>(list));
    }


    @ApiOperation(value = "交流中心新增")
    @PostMapping("/weixin/addClearingHouse")
    public AjaxResults addClearingHouse(KClearinghouse kClearinghouse)
    {
        Long id = SecurityUtils.getLoginUser().getUser().getUserId();
        Assert.notNull(id, "获取用户id失败！");
        kClearinghouse.setCreateBy(String.valueOf(id)   );
        return AjaxResults.success( kClearinghouseService.insertKClearinghouse(kClearinghouse));
    }


    /**
     * 交流中心查询详细信息
     */
    @ApiOperation(value = "交流中心查询详细信息")
    @GetMapping(value = "/weixin/clearingHouse/{id}")
    public AjaxResults getInfo(@PathVariable("id") String id)
    {
        KClearinghouse kClearinghouse=kClearinghouseService.selectKClearinghouseByIdForApp(id);

        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        Assert.notNull(userId, "获取用户id失败！");
        //是否点赞
        KClearinghouseLike kClearinghouseLike =new KClearinghouseLike();
        kClearinghouseLike.setClearinghouseId(String.valueOf(id));
        kClearinghouseLike.setIsLike((long) 1);
        kClearinghouseLike.setUserId(String.valueOf(userId));
        List<KClearinghouseLike> kClearinghouseLikes=kClearinghouseLikeService.selectKClearinghouseLikeList(kClearinghouseLike);
        if(kClearinghouseLikes.size()>0){
            kClearinghouse.setIsLike(true);
        }else {
            kClearinghouse.setIsLike(false);
        }
//        KClearinghouseComment kClearinghouseComment =new KClearinghouseComment();
//        kClearinghouseComment.setClearinghouseId(id);
//        List<KClearinghouseComment>   kClearinghouseCommentList = kClearinghouseCommentService.selectKClearinghouseCommentList(kClearinghouseComment);
//        kClearinghouse.setKClearinghouseCommentList(kClearinghouseCommentList);
        return AjaxResults.success(kClearinghouse);
    }


    /**
     *
     * 新增评论
     */
    @ApiOperation(value = "新增评论")
    @PostMapping(value = "/weixin/addComment")
    public AjaxResults addComment(KClearinghouseComment kClearinghouseComment)
    {

        return AjaxResults.success(kClearinghouseCommentService.insertKClearinghouseComment(kClearinghouseComment));
    }


    /**
     *
     * 新增评论回复
     */
//    @ApiOperation(value = "新增评论回复---废弃")
    @GetMapping(value = "/weixin/addCommentReply")
    public AjaxResults addCommentReply(KClearinghouseCommentReply kClearinghouseCommentReply)
    {

        return AjaxResults.success(kClearinghouseCommentReplyService.insertKClearinghouseCommentReply(kClearinghouseCommentReply));
    }


    @ApiOperation(value = "根据交流id获取回复带分页--前端只穿clearinghouseId  ")
    @GetMapping("/weixin/getCommentById")
    public AjaxResults<List<KClearinghouse>> getCommentById(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,KClearinghouseComment kClearinghouseComment)
    {
        PageHelper.startPage(pageNum, pageSize);
        kClearinghouseComment.setState(String.valueOf(1));
        List<KClearinghouseComment>   kClearinghouseCommentList=kClearinghouseCommentService.selectKClearinghouseCommentListForApp(kClearinghouseComment);
        return AjaxResults.success(new PageSerializable<>(kClearinghouseCommentList));
    }

    @ApiOperation(value = "点赞取消点赞")
    @GetMapping("/weixin/LikeOrCancel/{id}")
    public AjaxResults LikeOrCancel(@PathVariable("id") String id)
    {
        //获取用户信息
        Assert.notNull(id, "获取帖子id失败！");

        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        Assert.notNull(userId, "获取用户信息失败！");

        //先查询是否有记录  没有则新增 有则更改状态
        //是否点赞
        KClearinghouseLike kClearinghouseLike =new KClearinghouseLike();
        kClearinghouseLike.setClearinghouseId(String.valueOf(id));
        kClearinghouseLike.setUserId(String.valueOf(userId));
        List<KClearinghouseLike> kClearinghouseLikes=kClearinghouseLikeService.selectKClearinghouseLikeList(kClearinghouseLike);
        if(kClearinghouseLikes.size()>0){
            if(kClearinghouseLikes.get(0).getIsLike()==0){
                kClearinghouseLikes.get(0).setIsLike((long) 1);
            }else {
                kClearinghouseLikes.get(0).setIsLike((long) 0);
            }
            kClearinghouseLikeService.updateKClearinghouseLike(kClearinghouseLikes.get(0));
        }else {
            KClearinghouseLike kClearinghouseLike1 =new KClearinghouseLike();
            kClearinghouseLike1.setIsLike((long) 1);
            kClearinghouseLike1.setClearinghouseId(id);
            kClearinghouseLike1.setUserId(String.valueOf(userId));
            kClearinghouseLikeService.insertKClearinghouseLike(kClearinghouseLike1);
        }

        return AjaxResults.success();
    }

}
